﻿Public Class SALE
    Dim connect As CONDB = CONDB.NewConnection
    Private SALEID As Integer
    Private CUSID As Integer
    Private AGENTID As Integer
    Private PRC As String
    Private QTY As String
    Private AMOUNT As String
    Private PAYTYPE As String
    Private DOCDATE As Date
    Private REMARK As String
    Private USREQ As String
    Private SELECTSALE = Nothing
    Property USREQ_() As String
        Get
            Return USREQ
        End Get
        Set(value As String)
            USREQ = value
        End Set
    End Property
    Property SALEID_() As Integer
        Get
            Return SALEID
        End Get
        Set(value As Integer)
            SALEID = value
        End Set
    End Property
    Property CUSID_() As Integer
        Get
            Return CUSID
        End Get
        Set(value As Integer)
            CUSID = value
        End Set
    End Property
    Property AGENTID_() As Integer
        Get
            Return AGENTID
        End Get
        Set(value As Integer)
            AGENTID = value
        End Set
    End Property
    Property PRC_() As String
        Get
            Return PRC
        End Get
        Set(value As String)
            PRC = value
        End Set
    End Property
    Property QTY_() As String
        Get
            Return QTY
        End Get
        Set(value As String)
            QTY = value
        End Set
    End Property
    Property AMOUNT_() As String
        Get
            Return AMOUNT
        End Get
        Set(value As String)
            AMOUNT = value
        End Set
    End Property
    Property PAYTYPE_() As String
        Get
            Return PAYTYPE
        End Get
        Set(value As String)
            PAYTYPE = value
        End Set
    End Property

    Property DOCDATE_() As String
        Get
            Return DOCDATE
        End Get
        Set(value As String)
            DOCDATE = value
        End Set
    End Property

    Property REMARK_() As String
        Get
            Return REMARK
        End Get
        Set(value As String)
            REMARK = value
        End Set
    End Property

    Public Function insertSALE(dgv As DevComponents.DotNetBar.SuperGrid.SuperGridControl, amountx As String, usreqx As String, combobox As String) As String
        connect = CONDB.NewConnection()
        connect.BeginTrans()
        Dim lastid As Integer
        Try
            Dim sql As String
            sql = "INSERT INTO cpm.salemain"
            sql += "(`cusid`,`agentid`,`docdate`,`credit`,`usreq`)"
            sql += "VALUES ("
            sql += "'" & CUSID & "',"
            sql += "'" & AGENTID & "',"
            sql += "current_timestamp(),"
            sql += "'" & PAYTYPE & "',"
            sql += "'" & USREQ & "') ;SELECT LAST_INSERT_ID()"
            lastid = connect.ExecuteScalar(sql)
            sql = ""
            For i As Integer = 0 To dgv.PrimaryGrid.Rows.Count - 1
                'If CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("prdcode").Value.ToString.Trim <> "" Then
                sql += "INSERT INTO cpm.sale "
                sql += "(`salemain`,`product`,`prc`,`qty`,`amount`,`paytype`)"
                sql += "VALUES ("
                sql += "'" & lastid & "',"
                sql += "" & CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("product").Value.ToString & ","
                sql += "" & CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคาขาย").Value.ToString & ","
                sql += "" & CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จำนวน").Value.ToString & ","
                sql += "" & CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคารวม").Value.ToString & ","
                sql += "'" & combobox & "' );"
                'amountx.Replace(",", "").Replace(".00", "") &
                'End If
                sql += "UPDATE cpm.masproduct SET `qty` = " & CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("qtytotal").Value.ToString & " WHERE `idmasproduct` = " & CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("product").Value.ToString & " ; "
                sql += "INSERT INTO cpm.hissale "
                sql += "(`qty`,`product`,`usreq`,`date`,`total`,`salemain`) "
                sql += "VALUES ("
                sql += "" & CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จำนวน").Value.ToString & ","
                sql += "" & CType(dgv.PrimaryGrid.Rows(i), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("product").Value.ToString & ","
                sql += "" & usreqx & ", "
                sql += "current_timestamp() , "
                sql += "" & amountx.Replace(",", "").Replace(".00", "") & ","
                sql += " " & lastid & ") ; "
            Next i
            If sql <> "" Then
                connect.ExecuteNonQuery(sql)
                connect.CommitTrans()
                connect.Dispose()
                'MsgBox("ท่านได้ทำการบันทึกข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox(ex.ToString)
        Finally
            connect.Dispose()
        End Try
        Return lastid
    End Function
End Class
